package com.simple.utils;

import cn.hutool.core.date.DateUtil;
import com.simple.dto.*;
import com.simple.pojo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName BeanConverterUtil
 * @Author SimpleTree
 **/
@Component
public class BeanConverterUtil {

    public Bill toBill(BillDTO billDTO) {
        Bill bill = new Bill();
        BeanUtils.copyProperties(billDTO, bill);
        bill.setCostTime(TimeUtil.parse(billDTO.getCostTime(),1));
        return bill;
    }

    public BillDTO toBillDTO(Bill bill) {
        BillDTO billDTO = new BillDTO();
        BeanUtils.copyProperties(bill, billDTO);
        billDTO.setUsername(bill.getUserId() == 2 ? "熊" : bill.getUserId() == 3 ? "园" : "");
        billDTO.setCreateTime(TimeUtil.format(bill.getCreateTime()));
        billDTO.setCostTime(TimeUtil.format(bill.getCostTime(),1));
        return billDTO;
    }

    public List<BillDTO> toBillDTO(List<Bill> bills) {
        return bills.stream().map(this::toBillDTO).collect(Collectors.toList());
    }

    public Fund toFund(FundDTO fundDTO) {
        Fund fund = new Fund();
        BeanUtils.copyProperties(fundDTO, fund);
        return fund;
    }

    public FundDTO toFundDTO(Fund fund) {
        FundDTO fundDTO = new FundDTO();
        BeanUtils.copyProperties(fund, fundDTO);
        fundDTO.setUsername(fund.getUserId() == 2 ? "熊" : fund.getUserId() == 3 ? "园" : "");
        fundDTO.setBuyTime(TimeUtil.format(fund.getBuyTime(),1));
        fundDTO.setSellTime(TimeUtil.format(fund.getSellTime(),1));
        fundDTO.setExpirationTime(TimeUtil.format(fund.getExpirationTime(),1));
        return fundDTO;
    }

    public List<FundDTO> toFundDTO(List<Fund> funds) {
        return funds.stream().map(this::toFundDTO).collect(Collectors.toList());
    }

    public Amount toAmount(AmountDTO amountDTO) {
        Amount amount = new Amount();
        BeanUtils.copyProperties(amountDTO, amount);
        return amount;
    }

    public AmountDTO toAmountDTO(Amount amount) {
        AmountDTO amountDTO = new AmountDTO();
        BeanUtils.copyProperties(amount, amountDTO);
        amountDTO.setUsername(amount.getUserId() == 2 ? "熊" : amount.getUserId() == 3 ? "园" : "");
        amountDTO.setCreateTime(DateUtil.format(amount.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
        amountDTO.setUpdateTime(DateUtil.format(amount.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
        return amountDTO;
    }

    public List<AmountDTO> toAmountDTO(List<Amount> amounts) {
        return amounts.stream().map(this::toAmountDTO).collect(Collectors.toList());
    }

    public Bank toBank(BankDTO bankDTO) {
        Bank bank = new Bank();
        BeanUtils.copyProperties(bankDTO, bank);
        return bank;
    }

    public BankDTO toBankDTO(Bank bank) {
        BankDTO bankDTO = new BankDTO();
        BeanUtils.copyProperties(bank, bankDTO);
        return bankDTO;
    }

    public List<BankDTO> toBankDTO(List<Bank> banks) {
        return banks.stream().map(this::toBankDTO).collect(Collectors.toList());
    }

    public Income toIncome(IncomeDTO incomeDTO) {
        Income income = new Income();
        BeanUtils.copyProperties(incomeDTO, income);
        return income;
    }

    public List<Income> toIncome(List<IncomeDTO> incomeDTOS) {
        return incomeDTOS.stream().map(this::toIncome).collect(Collectors.toList());
    }

    public IncomeDTO toIncomeDTO(Income income) {
        IncomeDTO incomeDTO = new IncomeDTO();
        BeanUtils.copyProperties(income, incomeDTO);
        return incomeDTO;
    }

    public List<IncomeDTO> toIncomeDTO(List<Income> incomes) {
        return incomes.stream().map(this::toIncomeDTO).collect(Collectors.toList());
    }

    public User toUser(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        return user;
    }

    public UserDTO toUserDTO(User user) {
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user,userDTO);
        return userDTO;
    }
}
